import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { GteArticleListApi } from "@/http/api/article";


const fetchArticles=createAsyncThunk(
  'searchArticle/fetchArticles',
  async({SearchKey,type},thunkAPI)=>{
    const params = {
      Filter: SearchKey,
      PageSize: 10,
      PageNum: 1,
      ArticleType: "",
    };
    const res: any = await GteArticleListApi(params);
    const state:any = thunkAPI.getState();
    console.log("文章列表",res.ArticleList, state.searchArticle.ArticleList);
    if (type) {
      const newArticle = state.searchArticle.ArticleList.concat(res.ArticleList);
      return newArticle
    } else {
       return res.ArticleList
    }
  }
)

export const searchArticle=createSlice({
  name:"searchArticle",
  initialState:{
    ArticleList:[],
    SearchTypeId:1
  },
  reducers:{
    SetSearchTypeId(state,action){
      state.SearchTypeId=action.payload
    }
  },
  extraReducers:(builder)=>{
    builder.addCase(fetchArticles.fulfilled,(state,action)=>{
      state.ArticleList=action.payload
    })
  }
})


export const {SetSearchTypeId} =searchArticle.actions

export default searchArticle.reducer


export {fetchArticles}